package cn.androidservice;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btnStart;
    private Button btnStop;
    private Button btnBind;
    private Button btnUnBind;
    private ServiceConnection serviceConnection;
    private MyBinder myBinder;
    private Button btnGetTime;
    private boolean isBind = false;
    private Button btnReverse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findView();
        StartOrStopService();
        BindOrUnBind();
    }

    private void BindOrUnBind() {
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.e("MainActivity", "onServiceConnected");
                myBinder = (MyBinder) iBinder;
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Log.e("MainActivity", "onServiceConnected");
            }

            @Override
            public void onBindingDied(ComponentName componentName) {
                Log.e("MainActivity", "onBindingDied");
            }
        };


        btnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                isBind = bindService(intent, serviceConnection, BIND_AUTO_CREATE);
            }
        });

        btnUnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(serviceConnection);
            }
        });


        btnGetTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isBind) {
                    String time = myBinder.getTime();
                    Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "请先绑定服务", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btnReverse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isBind) {
                    String s = "I Love Android";
                    String result = myBinder.Reverse(s);
                    Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "请先绑定服务", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void StartOrStopService() {
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                startService(intent);
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                stopService(intent);
            }
        });
    }

    private void findView() {
        btnStart = findViewById(R.id.btn_start);
        btnStop = findViewById(R.id.btn_stop);
        btnBind = findViewById(R.id.btn_bind);
        btnUnBind = findViewById(R.id.btn_unbind);
        btnGetTime = findViewById(R.id.btn_get_time);
        btnReverse = findViewById(R.id.btn_reverse);
    }
}
